package com.ruoyi.system.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.exception.CustomException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.stores.EquipmentLedger;
import com.ruoyi.system.domain.stores.MaintenanceList;
import com.ruoyi.system.mapper.MaintenanceListMapper;
import com.ruoyi.system.service.IEquipmentLedgerService;
import com.ruoyi.system.service.IMaintenanceListService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;


/**
 * @author GE-LV5
 * @create 2023/2/20 16:57
 */
@Service
public class MaintenanceListServiceImpl   extends ServiceImpl<MaintenanceListMapper, MaintenanceList> implements IMaintenanceListService {


    @Autowired
    MaintenanceListMapper maintenanceListMapper;

    @Override
    public int deleteRoleById(Long id) {
        return maintenanceListMapper.deleteMaintenanceListById(id);
    }

    /**
     * 添加
     * @param maintenanceList 设备维护
     * @return
     */
    @Override
    @Transactional
    public int insertMaintenanceList(MaintenanceList maintenanceList) {
        if (StringUtils.isNull(maintenanceList))
        {
            throw new CustomException("添加失败！");
        }
        maintenanceList.setCreateTime(DateUtils.getNowDate());
        maintenanceList.setCreateBy(SecurityUtils.getUsername());
        return maintenanceListMapper.insertMaintenanceList(maintenanceList);
    }
    @Autowired
    IEquipmentLedgerService equipmentLedgerService;
    /**
     * 修改
     * @param ordersCustomer 设备维护
     * @return
     */
    @Override
    @Transactional
    public int updateMaintenanceList(MaintenanceList ordersCustomer) {
        EquipmentLedger equipmentLedger = equipmentLedgerService.equipmentLedgerById(ordersCustomer.getId());
        ordersCustomer.setEquipmentLedgerId(equipmentLedger.getId());
        ordersCustomer.setDeviceName(equipmentLedger.getEquipmentName());
        ordersCustomer.setUpdateBy(SecurityUtils.getUsername());
        ordersCustomer.setUpdateTime(new Date());
        return maintenanceListMapper.updateMaintenanceList(ordersCustomer);
    }
}
